//
//  ViewController.m
//  0524练习1(网易新闻)
//
//  Created by zs on 16/5/24.
//  Copyright © 2016年 zs. All rights reserved.
//

#import "ViewController.h"
#import "HotViewController.h"
#import "VideoViewController.h"
#import "ToplineViewController.h"
#import "ScoietyViewController.h"
#import "ScienceViewController.h"
#import "ReaderViewController.h"

#define zsScreenW [UIScreen mainScreen].bounds.size.width
#define zsScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController () <UIScrollViewDelegate>
/**TopTitleScrollView*/
@property (nonatomic, strong) UIScrollView *TopTitleScrollView;
/**contentScrollView*/
@property (nonatomic, strong) UIScrollView *contentScrollView;
/**顶部btns*/
@property (nonatomic, strong) NSMutableArray *titleBtns;
/**选中的顶部btn*/
@property (nonatomic, strong) UIButton *selectbtn;
@end

@implementation ViewController

- (NSMutableArray *)titleBtns{

    if (!_titleBtns) {
        _titleBtns = [NSMutableArray array];
        
    }
    return _titleBtns;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTopTitleScrollView];
    [self setUpcontentScrollView];
    [self setUpAllChildController];
    [self setUpTitleBtn];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.TopTitleScrollView.showsHorizontalScrollIndicator = NO;
    
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
}



- (void)setUpAllChildController{
   
    // 头条
    ToplineViewController *t = [[ToplineViewController alloc] init];
    t.title = @"头条";
    [self addChildViewController:t];
    
    // 热点
    HotViewController *h = [[HotViewController alloc] init];
    h.title = @"热点";
    
    [self addChildViewController:h];
    
    // 视频
    VideoViewController *v = [[VideoViewController alloc] init];
    v.title = @"视频";
    [self addChildViewController:v];
    
    // 社会
    ScoietyViewController *s0 = [[ScoietyViewController alloc] init];
    s0.title = @"社会";
    [self addChildViewController:s0];
    
    // 订阅
    ReaderViewController *r = [[ReaderViewController alloc] init];
    r.title = @"订阅";
    [self addChildViewController:r];
    
    // 科技
    ScienceViewController *s = [[ScienceViewController alloc] init];
    s.title = @"科技";
    [self addChildViewController:s];
    

}

- (void)setUpTitleBtn{
    NSInteger count = self.childViewControllers.count;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = 100;
    CGFloat btnH = 40;
    
    for (int i = 0; i < count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btnX = i * btnW;
        UIViewController *vc = self.childViewControllers[i];
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [self.TopTitleScrollView addSubview:btn];
        [self.titleBtns addObject:btn];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [self btnselect:btn];
        }
    }
    
    self.TopTitleScrollView.contentSize = CGSizeMake(self.childViewControllers.count * btnW, 0);
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * zsScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.delegate = self;
}

//btn被选中后的一些设置
- (void)btnselect:(UIButton *)btn{
    self.selectbtn.transform = CGAffineTransformIdentity;
    [self.selectbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.selectbtn = btn;
    
    CGFloat offsetX = btn.center.x - zsScreenW * 0.5;
    if (offsetX < 0) {
        offsetX = 0;
    }
    CGFloat maxOffsetX = self.TopTitleScrollView.contentSize.width - zsScreenW;
    if (offsetX > maxOffsetX){
        offsetX = maxOffsetX;
    }
    
    [self.TopTitleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    

}

//导航条按钮点击事件
- (void)btnClick:(UIButton *)btn{
    
    [self btnselect:btn];
    //点击按钮后下面的内容模块也偏移到相应位置
    UIViewController *vc = self.childViewControllers[btn.tag];
    vc.view.frame = CGRectMake(btn.tag * zsScreenW, 0, self.contentScrollView.bounds.size.width, self.contentScrollView.bounds.size.height);
    self.contentScrollView.contentOffset = CGPointMake(btn.tag * zsScreenW, 0);
    [self.contentScrollView addSubview:vc.view];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger page = self.contentScrollView.contentOffset.x / zsScreenW;
//    NSLog(@"%zd",page);
    UIButton *btn = self.titleBtns[page];
    [self btnselect:btn];
    UIViewController *vc = self.childViewControllers[page];
    vc.view.frame = CGRectMake(page * zsScreenW, 0, zsScreenW, self.contentScrollView.bounds.size.height);
    [self.contentScrollView addSubview:vc.view];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger leftI = scrollView.contentOffset.x / zsScreenW;
    NSInteger rightI = leftI + 1;
//    NSLog(@"%zd",leftI);
    UIButton *leftBtn = self.titleBtns[leftI];
    UIButton *rightBtn = nil;
    NSInteger count = self.titleBtns.count;
    
    if (rightI < count) {
        
        rightBtn = self.titleBtns[leftI + 1];
    }
    
//    NSLog(@"%f",self.contentScrollView.contentSize.width);
    NSLog(@"%f",self.contentScrollView.contentOffset.x/zsScreenW);
    CGFloat offsetR = scrollView.contentOffset.x / zsScreenW - leftI;
    CGFloat offsetL = 1 - offsetR;
    
    
    
    [leftBtn setTitleColor:[UIColor colorWithRed:offsetL green:0 blue:0 alpha:1] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:offsetR green:0 blue:0 alpha:1] forState:UIControlStateNormal];
}

//初始化导航条
- (void)setUpTopTitleScrollView{
    
    self.TopTitleScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, zsScreenW, 40)];
    
    [self.view addSubview:self.TopTitleScrollView];
}

//初始化内容的scrollView
- (void)setUpcontentScrollView{
    self.contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 104, zsScreenW, zsScreenH - 104)];
//    self.contentScrollView.backgroundColor = [UIColor blueColor];
    
    [self.view addSubview:self.contentScrollView];

}
@end
